﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevelopTool.Common.Properties;
using DevelopTool.Model;
using ICSharpCode.TextEditor;
using Sunny.UI;

namespace DevelopTool.Common
{
    /// <summary>
    /// 主题帮助类
    /// </summary>
    public class ThemHelper
    {
        /// <summary>
        /// 自定义黑色 56, 56, 56
        /// </summary>
        public static Color CustomBackColor_Black = Color.FromArgb(56, 56, 56);

        /// <summary>
        /// 自定义白色 240, 240, 240
        /// </summary>
        public static Color CustomBackColor_White = Color.FromArgb(240, 240, 240);


        /// <summary>
        /// 设置系统主题
        /// </summary>
        public static void SetSystemThem(Control control = null)
        {
            try
            {
                List<Control> controls = new List<Control>();
                if (control == null)
                {
                    //从配置文件读取主题配色
                    UIStyles.InitColorful(Setting.Theme.BackColor, Setting.Theme.ForeColor);

                    UIStyles.DPIScale = true;
                    UIStyles.GlobalFont = true;
                    UIStyles.GlobalFontName = Setting.Theme.FontFamily;
                    UIStyles.GlobalFontScale = Setting.Theme.FontSize;
                    UIStyles.SetDPIScale();

                    controls = Application.OpenForms.Cast<Control>().Where(it => it.Visible).ToList();
                }
                else
                {
                    controls.Add(control);
                }


                //List<Control> controls = Application.OpenForms.Cast<Control>().Where(it => it.Visible).ToList();
                //if (control != null && !controls.Exists(it => it.GetType() == control.GetType()))
                //{
                //    controls.Add(control);
                //}

                SetSunnyUIControlThem(controls);
            }
            catch (Exception ex)
            {
                // LogHelper.WriteErrLog(ex,false);
            }
        }


        /// <summary>
        /// 设置SunnyUI控件背景颜色、文字颜色、边框颜色等
        /// </summary>
        private static void SetSunnyUIControlThem(List<Control> controls)
        {
            if (controls == null || controls.Count <= 0) return;

            UIMenuStyle uIMenuStyle = Setting.Theme.ControlThemColor;

            foreach (Control form in controls)
            {
                form.Font = new Font(Setting.Theme.FontFamily, Setting.Theme.FontSize / 10, FontStyle.Regular);

                switch (uIMenuStyle)
                {
                    case UIMenuStyle.Black:
                        form.BackColor = CustomBackColor_Black;
                        form.ForeColor = Color.White;
                        break;
                    case UIMenuStyle.White:
                        form.BackColor = CustomBackColor_White;
                        form.ForeColor = Color.Black;
                        break;
                }

                if (form is UIForm uIForm)
                {
                    uIForm.Invoke(new Action(() =>
                    {
                        try
                        {
                            uIForm.ShowIcon = true;
                            uIForm.ShowTitleIcon = true;
                            uIForm.Icon = GlobalData.MainForm.Icon;
                        }
                        catch { }
                    }));


                    uIForm.RectColor = Setting.Theme.BackColor;
                    uIForm.TitleColor = Setting.Theme.BackColor;

                    switch (uIMenuStyle)
                    {
                        case UIMenuStyle.Black:
                            uIForm.BackColor = CustomBackColor_Black;
                            uIForm.ForeColor = Color.White;
                            break;
                        case UIMenuStyle.White:
                            uIForm.BackColor = CustomBackColor_White;
                            uIForm.ForeColor = Color.Black;
                            break;
                    }
                }

                if (form is UIPage uIPage)
                {
                    uIPage.RectColor = Setting.Theme.BackColor;

                    uIPage.Invoke(new Action(() =>
                    {
                        try
                        {
                            uIPage.ShowIcon = true;
                            uIPage.Icon = GlobalData.MainForm.Icon;
                        }
                        catch { }
                    }));
                }

                List<Control> ctrls = form.GetAllControls();

                foreach (Control control in ctrls)
                {
                    // control.Font= new Font(Setting.Theme.FontFamily, Setting.Theme.FontSize / 10, FontStyle.Regular);

                    if (control is UIPage uIPage2)
                    {
                        uIPage2.RectColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                uIPage2.BackColor = CustomBackColor_Black;
                                uIPage2.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                uIPage2.BackColor = CustomBackColor_White;
                                uIPage2.ForeColor = Color.Black;
                                break;
                        }
                    }

                    if (control is UINavBar navBar)
                    {
                        navBar.MenuStyle = uIMenuStyle;
                    }


                    if (control is UITabControl tabControl)
                    {
                        tabControl.MenuStyle = uIMenuStyle;
                        tabControl.TabSelectedForeColor = Setting.Theme.BackColor;
                        tabControl.TabSelectedHighColor = Setting.Theme.BackColor;

                        foreach (TabPage tabPage in tabControl.TabPages)
                        {
                            switch (uIMenuStyle)
                            {
                                case UIMenuStyle.Black:
                                    tabPage.BackColor = CustomBackColor_Black;
                                    break;
                                case UIMenuStyle.White:
                                    tabPage.BackColor = CustomBackColor_White;
                                    break;
                            }
                        }
                    }


                    if (control is UITabControlMenu tabControlMenu)
                    {
                        tabControlMenu.MenuStyle = uIMenuStyle;
                        foreach (TabPage tabPage in tabControlMenu.TabPages)
                        {
                            switch (uIMenuStyle)
                            {
                                case UIMenuStyle.Black:
                                    tabPage.BackColor = CustomBackColor_Black;
                                    break;
                                case UIMenuStyle.White:
                                    tabPage.BackColor = CustomBackColor_White;
                                    break;
                            }
                        }
                    }


                    if (control is UINavMenu navMenu)
                    {
                        navMenu.MenuStyle = uIMenuStyle;

                        navMenu.SelectedForeColor = Setting.Theme.BackColor;
                        navMenu.SelectedHighColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                navMenu.HoverColor = Color.Gray;
                                break;
                            case UIMenuStyle.White:
                                navMenu.HoverColor = Color.Silver;
                                break;
                        }

                    }

                    if (control is UIButton button)
                    {
                        button.RadiusSides = UICornerRadiusSides.None;
                    }


                    if (control is UISymbolButton symbolButton)
                    {
                        symbolButton.RadiusSides = UICornerRadiusSides.None;
                        symbolButton.FillDisableColor = Color.DarkGray;

                        symbolButton.FillColor = Setting.Theme.BackColor;
                        symbolButton.FillHoverColor = GlobalData.MainForm.ControlBoxFillHoverColor;
                        symbolButton.RectColor = Setting.Theme.BackColor;
                        symbolButton.RectHoverColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                symbolButton.LightColor = CustomBackColor_Black;
                                break;
                            case UIMenuStyle.White:
                                symbolButton.LightColor = CustomBackColor_White;
                                break;
                        }
                    }


                    if (control is UILabel label)
                    {
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                label.BackColor = CustomBackColor_Black;
                                label.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                label.BackColor = CustomBackColor_White;
                                label.ForeColor = Color.Black;
                                break;
                        }
                    }

                    if (control is UITitlePanel titlePanel)
                    {
                        titlePanel.RectColor = Setting.Theme.BackColor;
                        titlePanel.TitleColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                titlePanel.FillColor = CustomBackColor_Black;
                                titlePanel.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                titlePanel.FillColor = CustomBackColor_White;
                                titlePanel.ForeColor = Color.Black;
                                break;
                        }
                    }



                    if (control is UIComboBox comboBox)
                    {
                        comboBox.RadiusSides = UICornerRadiusSides.None;
                        
                        comboBox.ItemRectColor = Setting.Theme.BackColor;
                        comboBox.ItemHoverColor = GlobalData.MainForm.ControlBoxFillHoverColor;
                        comboBox.ItemSelectBackColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                comboBox.FillColor = CustomBackColor_Black;
                                comboBox.ForeColor = Color.White;

                                comboBox.ItemFillColor = CustomBackColor_Black;
                                comboBox.ItemForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                comboBox.FillColor = CustomBackColor_White;
                                comboBox.ForeColor = Color.Black;

                                comboBox.ItemFillColor = CustomBackColor_White;
                                comboBox.ItemForeColor = Color.Black;

                                break;
                        }
                    }


                    if (control is UIComboDataGridView comboDataGridView)
                    {
                        comboDataGridView.RadiusSides = UICornerRadiusSides.None;
                        comboDataGridView.RectDisableColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                comboDataGridView.FillColor = CustomBackColor_Black;
                                comboDataGridView.ForeColor = Color.White;

                                comboDataGridView.FillDisableColor = Color.FromArgb(74, 74, 74);
                                comboDataGridView.ForeDisableColor = Color.Gainsboro;

                                break;
                            case UIMenuStyle.White:
                                comboDataGridView.FillColor = CustomBackColor_White;
                                comboDataGridView.ForeColor = Color.Black;

                                comboDataGridView.FillDisableColor = Color.Gainsboro;
                                comboDataGridView.ForeDisableColor = Color.FromArgb(64, 64, 64);
                                break;
                        }
                    }


                    if (control is UIComboTreeView comboTreeView)
                    {
                        comboTreeView.RadiusSides = UICornerRadiusSides.None;
                        comboTreeView.RectDisableColor = Setting.Theme.BackColor;
                        comboTreeView.RectColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                comboTreeView.FillColor = CustomBackColor_Black;
                                comboTreeView.ForeColor = Color.White;

                                comboTreeView.FillDisableColor = Color.FromArgb(74, 74, 74);
                                comboTreeView.ForeDisableColor = Color.Gainsboro;
                                break;
                            case UIMenuStyle.White:
                                comboTreeView.FillColor = CustomBackColor_White;
                                comboTreeView.ForeColor = Color.Black;

                                comboTreeView.FillDisableColor = Color.Gainsboro;
                                comboTreeView.ForeDisableColor = Color.FromArgb(64, 64, 64);
                                break;
                        }
                    }


                    if (control is UIGroupBox groupBox)
                    {
                        groupBox.RectColor = Setting.Theme.BackColor;
                        groupBox.Style = UIStyle.Custom;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                groupBox.FillColor = CustomBackColor_Black;
                                groupBox.FillColor2 = CustomBackColor_Black;
                                groupBox.ForeColor = Color.White;

                                break;
                            case UIMenuStyle.White:
                                groupBox.FillColor = CustomBackColor_White;
                                groupBox.ForeColor = Color.Black;

                                break;
                        }
                    }


                    if (control is UIIntegerUpDown integerUpDown)
                    {
                        integerUpDown.RadiusSides = UICornerRadiusSides.None;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                integerUpDown.FillColor = CustomBackColor_Black;
                                integerUpDown.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                integerUpDown.FillColor = CustomBackColor_White;
                                integerUpDown.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is UIRichTextBox richTextBox)
                    {
                        richTextBox.RectColor = Setting.Theme.BackColor;
                        richTextBox.ScrollBarColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                richTextBox.FillColor = CustomBackColor_Black;
                                richTextBox.FillColor2 = CustomBackColor_Black;
                                richTextBox.ForeColor = Color.White;

                                richTextBox.ScrollBarBackColor = Color.FromArgb(66, 66, 66);
                                break;
                            case UIMenuStyle.White:
                                richTextBox.FillColor = CustomBackColor_White;
                                richTextBox.FillColor2 = CustomBackColor_White;
                                richTextBox.ForeColor = Color.Black;

                                richTextBox.ScrollBarBackColor = Color.Gray;
                                break;
                        }
                    }


                    if (control is UITextBox textBox)
                    {
                        // textBox.RadiusSides = UICornerRadiusSides.None;
                        textBox.RectColor = Setting.Theme.BackColor;
                        textBox.RectReadOnlyColor = Setting.Theme.BackColor;
                        textBox.RectDisableColor = Setting.Theme.BackColor;

                        textBox.ButtonFillColor = Setting.Theme.BackColor;
                        textBox.ButtonFillHoverColor = GlobalData.MainForm.ControlBoxFillHoverColor;
                        textBox.ButtonRectColor = Setting.Theme.BackColor;
                        textBox.ButtonRectHoverColor = Setting.Theme.BackColor;

                        textBox.ScrollBarColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                textBox.FillColor = CustomBackColor_Black;
                                textBox.ForeColor = Color.White;
                                textBox.SymbolColor = Color.White;

                                textBox.FillReadOnlyColor = Color.FromArgb(74, 74, 74);
                                textBox.ForeReadOnlyColor = Color.Gainsboro;

                                textBox.FillDisableColor = Color.FromArgb(74, 74, 74);
                                textBox.ForeDisableColor = Color.Gainsboro;

                                textBox.ScrollBarBackColor = Color.FromArgb(66, 66, 66);

                                break;
                            case UIMenuStyle.White:
                                textBox.FillColor = CustomBackColor_White;
                                textBox.ForeColor = Color.Black;
                                textBox.SymbolColor = Color.Black;

                                textBox.FillReadOnlyColor = Color.Gainsboro;
                                textBox.ForeReadOnlyColor = Color.FromArgb(64, 64, 64);

                                textBox.FillDisableColor = Color.Gainsboro;
                                textBox.ForeDisableColor = Color.FromArgb(64, 64, 64);

                                textBox.ScrollBarBackColor = CustomBackColor_White;

                                textBox.ScrollBarBackColor = Color.Gray;
                                break;
                        }
                    }


                    if (control is UICheckBox checkBox)
                    {
                        checkBox.RadiusSides = UICornerRadiusSides.None;
                        checkBox.CheckBoxColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                checkBox.BackColor = CustomBackColor_Black;
                                checkBox.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                checkBox.BackColor = CustomBackColor_White;
                                checkBox.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is UIPanel panel)
                    {
                        panel.RadiusSides = UICornerRadiusSides.None;
                        panel.RectColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                panel.FillColor = CustomBackColor_Black;
                                panel.FillColor2 = CustomBackColor_Black;
                                panel.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                panel.FillColor = CustomBackColor_White;
                                panel.FillColor2 = CustomBackColor_White;
                                panel.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is UIRadioButton radioButton)
                    {
                        radioButton.RadioButtonColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                radioButton.ForeColor = Color.White;
                                radioButton.BackColor = CustomBackColor_Black;

                                break;
                            case UIMenuStyle.White:
                                radioButton.BackColor = CustomBackColor_White;
                                radioButton.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is UIRadioButtonGroup radioButtonGroup)
                    {
                        radioButtonGroup.RectColor = Setting.Theme.BackColor;
                        radioButtonGroup.RadioButtonColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                radioButtonGroup.FillColor = CustomBackColor_Black;
                                radioButtonGroup.FillColor2 = CustomBackColor_Black;
                                radioButtonGroup.BackColor = CustomBackColor_Black;
                                radioButtonGroup.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                radioButtonGroup.FillColor = CustomBackColor_White;
                                radioButtonGroup.FillColor2 = CustomBackColor_White;
                                radioButtonGroup.BackColor = CustomBackColor_White;
                                radioButtonGroup.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is PictureBox pictureBox)
                    {
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                pictureBox.BackColor = CustomBackColor_Black;
                                break;
                            case UIMenuStyle.White:
                                pictureBox.BackColor = CustomBackColor_White;
                                break;
                        }
                    }


                    if (control is UIWaitingBar waitingBar)
                    {
                        waitingBar.RectColor = Setting.Theme.BackColor;
                        waitingBar.ForeColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                waitingBar.FillColor = CustomBackColor_Black;
                                break;
                            case UIMenuStyle.White:
                                waitingBar.FillColor = CustomBackColor_White;
                                break;
                        }
                    }


                    if (control is UITableLayoutPanel tableLayoutPanel)
                    {
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                tableLayoutPanel.BackColor = CustomBackColor_Black;
                                break;
                            case UIMenuStyle.White:
                                tableLayoutPanel.BackColor = CustomBackColor_White;
                                break;
                        }
                    }


                    if (control is UIColorPicker colorPicker)
                    {
                        colorPicker.RectColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                colorPicker.FillColor = CustomBackColor_Black;
                                colorPicker.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                colorPicker.FillColor = CustomBackColor_White;
                                colorPicker.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is UITrackBar trackBar)
                    {
                        trackBar.RectColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                trackBar.FillColor = CustomBackColor_Black;
                                trackBar.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                trackBar.FillColor = CustomBackColor_White;
                                trackBar.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is UIRuler ruler)
                    {
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                ruler.BackColor = CustomBackColor_Black;
                                ruler.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                ruler.BackColor = CustomBackColor_White;
                                ruler.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is UIListBox listBox)
                    {
                        listBox.RectColor = Setting.Theme.BackColor;
                        listBox.HoverColor = Color.Gray;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                listBox.FillColor = CustomBackColor_Black;
                                listBox.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                listBox.FillColor = CustomBackColor_White;
                                listBox.ForeColor = Color.Black;
                                break;
                        }
                    }


                    if (control is UIImageListBox imageListBox)
                    {
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                imageListBox.Style = UIStyle.Black;
                                imageListBox.FillColor = CustomBackColor_Black;
                                imageListBox.ForeColor = Color.White;

                                break;
                            case UIMenuStyle.White:
                                imageListBox.Style = UIStyle.Gray;
                                imageListBox.FillColor = CustomBackColor_White;
                                imageListBox.ForeColor = Color.Black;
                                break;
                        }
                        imageListBox.RectColor = Setting.Theme.BackColor;
                        imageListBox.HoverColor = Color.Gray;
                        imageListBox.ItemSelectBackColor = Setting.Theme.BackColor;
                    }


                    if (control is UIProcessBar processBar)
                    {
                        processBar.RectColor = Setting.Theme.BackColor;
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                processBar.ForeColor = CustomBackColor_White;
                                processBar.FillColor = CustomBackColor_Black;
                                break;
                            case UIMenuStyle.White:
                                processBar.ForeColor = CustomBackColor_Black;
                                processBar.FillColor = CustomBackColor_White;
                                break;
                        }
                    }


                    //if(control is UIScrollingText scrollingText)
                    //{
                    //    scrollingText.ForeColor = Setting.Theme.BackColor;
                    //    scrollingText.RectColor = Setting.Theme.BackColor;
                    //    switch(uIMenuStyle)
                    //    {
                    //        case UIMenuStyle.Black:
                    //            scrollingText.FillColor = CustomBackColor_Black;
                    //            break;
                    //        case UIMenuStyle.White:
                    //            scrollingText.FillColor = CustomBackColor_White;
                    //            break;
                    //    }
                    //}



                    if (control is TextEditorControl textEditorControl)
                    {
                        string themName = "Default";
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                themName = "Black";
                                break;
                            case UIMenuStyle.White:
                                themName = "Default";
                                break;
                        }
                        if (textEditorControl.Document.ThemeName != themName)
                        {
                            textEditorControl.BeginUpdate();
                            textEditorControl.SetHighlighting(themName, textEditorControl.Document.HighlightingStrategy.Name);
                            textEditorControl.EndUpdate();
                            textEditorControl.Invalidate();
                        }
                    }



                    if (control is UIDatetimePicker datetimePicker)
                    {
                        datetimePicker.RectColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                datetimePicker.FillColor = CustomBackColor_Black;
                                datetimePicker.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                datetimePicker.FillColor = CustomBackColor_White;
                                datetimePicker.ForeColor = Color.Black;
                                break;
                        }
                    }



                    if (control is UITreeView treeView)
                    {
                        treeView.RectColor = Setting.Theme.BackColor;
                        treeView.SelectedForeColor = Setting.Theme.BackColor;

                        treeView.ScrollBarRectColor = Setting.Theme.BackColor;
                        treeView.ScrollBarColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                treeView.FillColor = CustomBackColor_Black;
                                treeView.ForeColor = Color.White;
                                treeView.HoverColor = Color.Gray;
                                treeView.SelectedColor = Color.FromArgb(36, 36, 36);
                                break;
                            case UIMenuStyle.White:
                                treeView.FillColor = CustomBackColor_White;
                                treeView.ForeColor = Color.Black;
                                treeView.HoverColor = Color.Silver;
                                treeView.SelectedColor = Color.FromArgb(250, 250, 250);
                                break;
                        }
                    }



                    if (control is DataGridView dgv)
                    {
                        dgv.GridColor = Setting.Theme.BackColor;

                        //列头样式
                        DataGridViewCellStyle columnHeadersDefaultCellStyle = dgv.ColumnHeadersDefaultCellStyle;
                        columnHeadersDefaultCellStyle.BackColor = Setting.Theme.BackColor;
                        columnHeadersDefaultCellStyle.ForeColor = Color.White;
                        columnHeadersDefaultCellStyle.Font = new Font("宋体", 10F, FontStyle.Regular);

                        dgv.ColumnHeadersHeight = 30;

                        dgv.BorderStyle = BorderStyle.FixedSingle;
                        dgv.CellBorderStyle = DataGridViewCellBorderStyle.Single;
                        dgv.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;


                        //默认单元格样式
                        DataGridViewCellStyle defaultCellStyle = dgv.DefaultCellStyle;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                dgv.BackgroundColor = CustomBackColor_Black;
                                dgv.ForeColor = Setting.Theme.BackColor;

                                //行
                                DataGridViewCellStyle rowsDefaultCellStyle = dgv.RowsDefaultCellStyle;
                                rowsDefaultCellStyle.BackColor = CustomBackColor_Black;
                                rowsDefaultCellStyle.ForeColor = Color.White;

                                //奇数行
                                DataGridViewCellStyle alternatingRowsDefaultCellStyle = dgv.AlternatingRowsDefaultCellStyle;
                                alternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(63, 63, 63);
                                alternatingRowsDefaultCellStyle.ForeColor = Color.White;

                                //行头
                                DataGridViewCellStyle rowHeadersDefaultCellStyle = dgv.RowHeadersDefaultCellStyle;
                                rowHeadersDefaultCellStyle.BackColor = Color.FromArgb(63, 63, 63);
                                rowHeadersDefaultCellStyle.ForeColor = Color.White;


                                defaultCellStyle.BackColor = CustomBackColor_Black;
                                defaultCellStyle.ForeColor = Color.White;

                                break;
                            case UIMenuStyle.White:

                                defaultCellStyle.BackColor = CustomBackColor_White;
                                defaultCellStyle.ForeColor = Color.Black;
                                break;
                        }

                        defaultCellStyle.SelectionForeColor = Color.White;
                        defaultCellStyle.SelectionBackColor = Color.Gray;
                    }


                    if (control is UIDataGridView dataGridView)
                    {
                        dataGridView.RectColor = Setting.Theme.BackColor;

                        dataGridView.ScrollBarRectColor = Setting.Theme.BackColor;
                        dataGridView.ScrollBarColor = Setting.Theme.BackColor;
                        dataGridView.GridColor = Setting.Theme.BackColor;

                        dataGridView.RowTemplate.Height = 23;

                        //列头样式
                        DataGridViewCellStyle columnHeadersDefaultCellStyle = dataGridView.ColumnHeadersDefaultCellStyle;

                        columnHeadersDefaultCellStyle.BackColor = Setting.Theme.BackColor;
                        columnHeadersDefaultCellStyle.ForeColor = Color.White;


                        foreach (DataGridViewColumn column in dataGridView.Columns)
                        {
                            //能点击获取内容的列样式设置
                            if (column.Tag != null && column.Tag is DataGridColumn dataGridColumn)
                            {
                                if (dataGridColumn.ColumnType.HasFlag(ColumnType.CanClick))//是否是点击获取值列
                                {
                                    column.HeaderCell.Style.ForeColor = Color.Blue;
                                    column.HeaderCell.Style.Font = new Font(Setting.Theme.FontFamily, Setting.Theme.FontSize / 10, FontStyle.Underline);
                                }

                                if (dataGridColumn.ColumnType.HasFlag(ColumnType.Important))//是否是重要列
                                {
                                    column.HeaderCell.Style.ForeColor = Color.Red;
                                    column.HeaderCell.Style.Font = new Font(Setting.Theme.FontFamily, Setting.Theme.FontSize / 10, FontStyle.Bold);
                                }
                            }
                        }

                        dataGridView.ColumnHeadersHeight = 30;

                        dataGridView.BorderStyle = BorderStyle.FixedSingle;
                        dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
                        dataGridView.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;


                        //默认单元格样式
                        DataGridViewCellStyle defaultCellStyle = dataGridView.DefaultCellStyle;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                dataGridView.BackgroundColor = CustomBackColor_Black;
                                dataGridView.ForeColor = Setting.Theme.BackColor;

                                //行
                                DataGridViewCellStyle rowsDefaultCellStyle = dataGridView.RowsDefaultCellStyle;
                                rowsDefaultCellStyle.BackColor = CustomBackColor_Black;
                                rowsDefaultCellStyle.ForeColor = Color.White;


                                //奇数行
                                DataGridViewCellStyle alternatingRowsDefaultCellStyle = dataGridView.AlternatingRowsDefaultCellStyle;
                                alternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(63, 63, 63);
                                alternatingRowsDefaultCellStyle.ForeColor = Color.White;
                                alternatingRowsDefaultCellStyle.Font = new Font(Setting.Theme.FontFamily, Setting.Theme.FontSize / 10, FontStyle.Regular);


                                //行头
                                DataGridViewCellStyle rowHeadersDefaultCellStyle = dataGridView.RowHeadersDefaultCellStyle;
                                rowHeadersDefaultCellStyle.BackColor = Color.FromArgb(63, 63, 63);
                                rowHeadersDefaultCellStyle.ForeColor = Color.White;

                                dataGridView.ScrollBarBackColor = Color.FromArgb(66, 66, 66);

                                defaultCellStyle.BackColor = CustomBackColor_Black;
                                defaultCellStyle.ForeColor = Color.White;

                                break;
                            case UIMenuStyle.White:
                                dataGridView.ScrollBarBackColor = Color.WhiteSmoke;

                                defaultCellStyle.BackColor = CustomBackColor_White;
                                defaultCellStyle.ForeColor = Color.Black;
                                break;
                        }

                        defaultCellStyle.SelectionForeColor = Setting.Theme.BackColor;
                        defaultCellStyle.SelectionBackColor = GlobalData.MainForm.ControlBoxFillHoverColor;
                    }



                    if (control is UIDataGridViewFooter dataGridViewFooter)
                    {
                        dataGridViewFooter.RectColor = Setting.Theme.BackColor;
                        dataGridViewFooter.ForeColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                dataGridViewFooter.FillColor = CustomBackColor_Black;


                                break;
                            case UIMenuStyle.White:
                                dataGridViewFooter.FillColor = CustomBackColor_White;
                                break;
                        }
                    }



                    if (control is UISplitContainer splitContainer)
                    {
                        splitContainer.ArrowColor = Setting.Theme.BackColor;
                        splitContainer.HandleHoverColor = GlobalData.MainForm.ControlBoxFillHoverColor;
                        splitContainer.BarColor = Color.Gray;
                    }



                    if (control is UIContextMenuStrip contextMenuStrip)
                    {
                        contextMenuStrip.Style = UIStyle.Custom;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                contextMenuStrip.BackColor = CustomBackColor_Black;
                                contextMenuStrip.ForeColor = Color.White;

                                break;
                            case UIMenuStyle.White:
                                contextMenuStrip.BackColor = CustomBackColor_White;
                                contextMenuStrip.ForeColor = Color.Black;
                                break;
                        }
                    }



                    if (control is MenuStrip menuStrip)
                    {
                        menuStrip.RenderMode = ToolStripRenderMode.System;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                menuStrip.BackColor = CustomBackColor_Black;
                                menuStrip.ForeColor = Color.White;
                                break;
                            case UIMenuStyle.White:
                                menuStrip.BackColor = Color.Silver;
                                menuStrip.ForeColor = Color.Black;
                                break;
                        }

                        foreach (ToolStripMenuItem item in menuStrip.Items)
                        {
                            item.Padding = new Padding(item.Padding.Left, item.Padding.Top, 20, item.Padding.Bottom);

                            switch (uIMenuStyle)
                            {
                                case UIMenuStyle.Black:
                                    item.ForeColor = Color.White;
                                    break;
                                case UIMenuStyle.White:
                                    item.ForeColor = Color.Black;
                                    break;
                            }
                        }
                    }


                    if (control is StatusStrip statusStrip)
                    {
                        statusStrip.BackColor = Setting.Theme.BackColor;
                        statusStrip.ForeColor = Color.White;
                    }

                    if (control is UIAvatar uIAvatar)
                    {
                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                uIAvatar.BackColor = CustomBackColor_Black;
                                uIAvatar.FillColor = CustomBackColor_Black;
                                break;
                            case UIMenuStyle.White:
                                uIAvatar.BackColor = CustomBackColor_White;
                                uIAvatar.FillColor = CustomBackColor_White;
                                break;
                        }
                    }

                    if (control is UIHeaderButton uIHeaderButton)
                    {

                        uIHeaderButton.CircleColor = Setting.Theme.BackColor;
                        uIHeaderButton.FillSelectedColor = Setting.Theme.BackColor;
                        uIHeaderButton.FillPressColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                uIHeaderButton.BackColor = CustomBackColor_Black;
                                uIHeaderButton.FillColor = CustomBackColor_Black;
                                uIHeaderButton.FillHoverColor = Color.Gray;

                                uIHeaderButton.ForeColor = CustomBackColor_White;
                                break;
                            case UIMenuStyle.White:
                                uIHeaderButton.BackColor = CustomBackColor_White;
                                uIHeaderButton.FillColor = CustomBackColor_White;
                                uIHeaderButton.FillHoverColor = Color.Silver;

                                uIHeaderButton.ForeColor = CustomBackColor_Black;
                                break;
                        }
                    }


                    if (control is UIFlowLayoutPanel uIFlowLayoutPanel)
                    {
                        uIFlowLayoutPanel.ScrollBarColor = Setting.Theme.BackColor;
                        uIFlowLayoutPanel.RectColor = Setting.Theme.BackColor;

                        switch (uIMenuStyle)
                        {
                            case UIMenuStyle.Black:
                                uIFlowLayoutPanel.BackColor = CustomBackColor_Black;
                                uIFlowLayoutPanel.FillColor = CustomBackColor_Black;
                                uIFlowLayoutPanel.FillColor2 = CustomBackColor_Black;

                                uIFlowLayoutPanel.ScrollBarBackColor = CustomBackColor_Black;

                                break;
                            case UIMenuStyle.White:
                                uIFlowLayoutPanel.BackColor = CustomBackColor_White;
                                uIFlowLayoutPanel.FillColor = CustomBackColor_White;
                                uIFlowLayoutPanel.FillColor2 = CustomBackColor_White;

                                uIFlowLayoutPanel.ScrollBarBackColor = CustomBackColor_White;
                                break;
                        }
                    }
                }
            }
        }


        /// <summary>
        /// 网页主题设置
        /// </summary>
        /// <param name="htmlBody">html body内容</param>
        /// <param name="htmlTitle">网页标题</param>
        /// <param name="defaultContent">内容为空时的占位内容 可为空</param>
        /// <returns></returns>
        public static string SetHtmlThem(string htmlBody, string htmlTitle, string defaultContent = "")
        {
            if (!string.IsNullOrEmpty(htmlTitle)) htmlTitle = "-" + htmlTitle;

            string backColor = "black";
            string foreColor = "white";
            string styleCss = string.Empty;

            UIMenuStyle uIMenuStyle = Setting.Theme.ControlThemColor;
            switch (uIMenuStyle)
            {
                case UIMenuStyle.Black:
                    backColor = CustomBackColor_Black.ToHTML();
                    foreColor = "white";
                    styleCss = Resource.github_markdown_dark;//从嵌入资源中读取css样式 https://github.com/sindresorhus/github-markdown-css
                    break;
                case UIMenuStyle.White:
                    backColor = CustomBackColor_White.ToHTML();
                    foreColor = "black";
                    styleCss = Resource.github_markdown_light;
                    break;
            }

            string top = $@"<!DOCTYPE html>
                        <html lang=""zh-CN"">
                        <head>
                            <meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">
                            <meta charset=""UTF-8"">
                            <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">                        
                            <meta name=""author"" content=""{GlobalData.Author}"">
                            <title>{GlobalData.AppName_zh}{htmlTitle}</title>
						<style>
	                        {styleCss}
                        </style>
                        </head>
                        <body style=""background-color: {backColor};color: {foreColor};""><article class=""markdown-body"">";

            string bottom = "</article></body></html>";

            //设置默认内容
            if (string.IsNullOrEmpty(htmlBody) && !string.IsNullOrEmpty(defaultContent)) htmlBody = $"<h5>{defaultContent}</h5>";

            return htmlBody.Contains("<html") ? htmlBody : top + htmlBody + bottom;
        }
    }
}
